<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>role管理</title>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">

    <!-- 上传图片框样式，不使用上传组件可以删除此样式 -->
    <style>
        .avatar-uploader .el-upload {
            border: 1px dashed #d9d9d9;
            border-radius: 6px;
            cursor: pointer;
            position: relative;
            overflow: hidden;
        }
        .avatar-uploader .el-upload:hover {
            border-color: #409EFF;
        }
        .avatar-uploader-icon {
            font-size: 28px;
            color: #8c939d;
            width: 150px;
            height: 70px;
            line-height: 150px;
            text-align: center;
        }
        .avatar {
            width: 150px;
            height: 70px;
            display: block;
        }

    </style>
</head>
<body class="hold-transition">
<div id="app">

    <div class="content-header">
        <h1>管理<small>role管理</small></h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>管理</el-breadcrumb-item>
            <el-breadcrumb-item>role管理</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <div class="app-container">
        <div class="box">
            <div class="filter-container">
                <el-button type="primary"  class="butT" @click="formVisible=true;pojo={};checkResource=[]">新增</el-button>
            </div>

            <el-table :data="tableData" border style="width: 100%">
                    <el-table-column prop="role.id" label="ID" width="150"></el-table-column>
                    <el-table-column prop="role.name" label="角色名称" width="200"></el-table-column>
                    <el-table-column label="角色" width="180">
                        <template slot-scope="scope" >
                            <el-select  placeholder="所属权限"  >
                                <el-option
                                        v-for="item in scope.row.resourceList"
                                        :key="item.id"
                                        :label="item.resName"
                                        :value="item.id">
                                </el-option>
                            </el-select>
                        </template>
                    </el-table-column>
                    <el-table-column label="操作"  >
                        <template slot-scope="scope">
                            <el-button @click="edit(scope.row.role.id)" size="mini" type="primary" size="small">修改</el-button>
                            <el-button @click="dele(scope.row.role.id)" size="mini" type="danger" size="small">删除</el-button>
                        </template>
                    </el-table-column>
            </el-table>
            <div class="add-form">
                <!--弹出窗口-->
                <el-dialog  title="编辑" :visible.sync="formVisible" >
                    <el-form label-width="80px">
                            <el-form-item label="角色名称"><el-input v-model="pojo.name"></el-input></el-form-item>

                             <el-form-item label="角色">
                            <el-checkbox :indeterminate="isIndeterminate" v-model="checkAll" @change="handleCheckAllChange">全选</el-checkbox>
                            <div style="margin: 15px 0;"></div>
                            <el-checkbox-group v-model="checkResource" @change="handleCheckedCitiesChange">
                                <el-checkbox v-for="item in resourceList" :label="item" :key="item.id">{{item.resName}}</el-checkbox>
                            </el-checkbox-group>
                            </el-input></el-form-item>

                            <el-form-item>
                                <el-button type="primary" @click="save()">保存</el-button>
                                <el-button @click="formVisible = false" >关闭</el-button>
                            </el-form-item>
                    </el-form>
                </el-dialog>
            </div>
        </div>
    </div>


</div>
</body>
<script src="/js/vue.js"></script>
<script src="/js/axios.js"></script>
<script src="/js/elementui.js"></script>
<script>
     new Vue({
        el: '#app',
        data(){
            return {
                tableData: [],
                searchMap: {},
                pojo: {},
                formVisible:false,
                checkAll: false,
                isIndeterminate: true,
                wholeRole:{},
                resourceList:[],
                checkResource:[]
            }
        },
        created(){
            this.fetchData();
            axios.get(`/resource/findAll.do`).then(response=>{
                this.resourceList=response.data;
            })
        },
        methods:{
            fetchData (){
                axios.get(`/role/findAllWholeRole.do?`).then(response => {
                    this.tableData = response.data;
                });
            },
            save (){
                this.wholeRole.role=this.pojo;
                this.wholeRole.resourceList=this.checkResource;
                axios.post(`/role/${this.pojo.id==null?'addWholeRole':'updateWholeRole'}.do`,this.wholeRole).then(response => {
                    this.fetchData (); //刷新列表
                    this.formVisible = false ;//关闭窗口
                });
            },
            edit (id){
                this.formVisible = true // 打开窗口
                axios.get(`/resource/findAll.do`).then(response=>{
                    this.resourceList=response.data;
                })
                // 调用查询
                axios.get(`/role/findWholeById.do?id=${id}`).then(response => {
                    this.pojo = response.data.role;
                    // this.imageUrl=this.pojo.image //显示图片  如页面有图片上传功能放开注释
                })
            },
            dele (id){
                this.$confirm('确定要删除此记录吗?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then( () => {
                    axios.get(`/role/deleteWholeRole.do?id=${id}`).then(response => {
                        this.fetchData (); //刷新列表
                    })
                })
            },
            handleCheckAllChange(val) {
                this.checkedCities = val ? this.resourceList : [];
                this.isIndeterminate = false;
            },
            handleCheckedCitiesChange(value) {
                let checkedCount = value.length;
                this.checkAll = checkedCount === this.resourceList.length;
                this.isIndeterminate = checkedCount > 0 && checkedCount < this.resourceList.length;
            }

        }
    })
</script>
</html>
